﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IBLL;
using Dtos;
using DAL;
using IDAL;

namespace BLL
{
    public class HomeworkBll:IHomeworkBll
    {
        private IHomeworkDal _H;
        public HomeworkBll(IHomeworkDal H) 
        {
        _H= H;
        }

        public async Task<int> AddHomeworkAsync(string title, DateTime publicshTime, DateTime endTime)
        {
            return await _H.AddAsync(new Model.Homework
            {
                title = title,
                publishTime = publicshTime,
                endTime = endTime
            });
        }

        public async Task<int> DeleteHomeworkAsync(Guid id)
        {
            var data = await _H.GetIdAll(id);
            return data != null ? await _H.AddAsync(data) : 0;
        }

        public async Task<int> EditHomeworkAsync(Guid id, string title, DateTime publicshTime, DateTime endTime)
        {
            var data = await _H.GetIdAll(id);
            if (data == null)
                return 0;
            data.title = title;
            data.publishTime = publicshTime;
            data.endTime = endTime;
            data.UpdateTime = DateTime.Now;
            return await _H.UpdateAsync(data);
        }

        public async Task<List<HomeworkDtos>> getAllHomework()
        {
            var data = await _H.GetAll();
            return data.Select(d => new HomeworkDtos
            {
                Id = d.Id,
               
                UpdateTime = DateTime.Now
            }).OrderByDescending(d => d.UpdateTime).ToList();
        }

        public async Task<List<HomeworkDtos>> getHomeworkByNameAsync(string title)
        {
            var data = await _H.GetDataByChoose(a => a.title.Contains(title));
            return data == null ? null : data.Select(m => new HomeworkDtos
            {
                Id = m.Id,
                Title = m.title,
                publishTime = m.publishTime,
                endTime = m.endTime,
                UpdateTime = m.UpdateTime,
            }).OrderByDescending(g => g.UpdateTime).ToList();
        }
        public Task<HomeworkDtos> GetHomeworkIdAsync(Guid id)
        {
            throw new NotImplementedException();
        }

        public async Task<bool> IsExistsAsync(string title)
        {
            var a = await _H.GetDataByChoose(m => m.title.Equals(title));
            return a.Count() > 0;
        }
    }
}
